<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class VideoDB extends BaseDB
{
    /**
     * 视频
     */

    public $tVideo = 'video';
    public $tPraise = 'u_zan';
    public $tComment = 'u_comment';

    public function __construct()
    {
    }

    /**
     * 一条记录
     * @param $oper1上拉2下拉，$type1果干2农产品
     */
    public function getOneByOper($wx,$id,$oper,$type)
    {
        $query = DB::table($this->tVideo)->where('del',0)->where('type',$type);
        if (!$id) {
            $models = $query->where('id','>=',$id)->orderBy('id','asc')->get();
        } else if ($oper==1) {
            $models = $query->where('id','>',$id)->orderBy('id','asc')->get();
        } else if ($oper==2) {
            $models = $query->where('id','<',$id)->orderBy('id','desc')->get();
        } else {
            return array('code'=>'-2','msg'=>'参数错误');
        }
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $model = $this->jsonToArr($models[0]);
        $zan = DB::table($this->tPraise)->where('del',0)->where('wx',$wx)->where('vid',$model['id'])->count();
        $zanTotal = DB::table($this->tPraise)->where('del',0)->where('vid',$model['id'])->count();
        $pl = DB::table($this->tComment)->where('del',0)->where('wx',$wx)->where('vid',$model['id'])->count();
        $plTotal = DB::table($this->tComment)->where('del',0)->where('vid',$model['id'])->count();
        $items = [
            'vid'=>$model['id'], 'vcover'=>$model['cover'], 'vlink'=>$model['link'],
            'juli'=>'距离10KM', 'zan'=>$zan, 'zanTotal'=>$zanTotal, 'pl'=>$pl, 'plTotal'=>$plTotal,
        ];
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 点赞列表
     */
    public function getPraiseIndex($wx)
    {
        $models = DB::table($this->tPraise)->where('del',0)->where('wx',$wx)
            ->skip(0)->take(10)->orderBy('id','desc')->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $items = array(); $models = $this->jsonToArr($models);
        foreach ($models as $model) {
            $addTime = date('Y-m-d',$model['created_at']);
            //视频信息
            $resOnes = DB::table($this->tVideo)->where('id',$model['vid'])
                ->skip(0)->take(1)->orderBy('id','desc')->get();
            $resOne = count($resOnes) ? $this->jsonToArr($resOnes[0]) : '';
            $cover = $resOne ? $resOne['cover'] : '';
            $link = $resOne ? $resOne['link'] : '';
            $items[] = [
                'id'=>$model['id'], 'wx'=>$model['wx'], 'addTime'=>$addTime,
                'vid'=>$model['vid'], 'cover'=>$cover, 'link'=>$link,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 获取点赞
     */
    public function getPraiseOne($wx,$vid)
    {
        return DB::table($this->tPraise)->where('del',0)->where('wx',$wx)->where('vid',$vid)->first();
    }

    /**
     * 点赞增加
     */
    public function setPraiseAdd($wx,$vid)
    {
        return DB::table($this->tPraise)->insertGetId(['wx'=>$wx,'vid'=>$vid,'created_at'=>time()]);
    }

    /**
     * 点赞移除
     */
    public function setPraiseDel($wx,$vid)
    {
        return DB::table($this->tPraise)->where('wx',$wx)->where('vid',$vid)->update(['del'=>1,'updated_at'=>time()]);
    }
}